<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;
use Qwadmin\Model\ProductModel;

class ProductController extends ComController
{
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $where = 'deleted = 0';
        $product = M('product');
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $product->where($where)->count();

        $list = $product->where($where)->limit($offset . ',' . $pagesize)->select();
        if(!empty($list)){
            foreach ($list as $k=> &$v) {
                $v['pro_type'] = ProductModel::CATEGORY[$v['pro_type']];
            }
        }
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
//        pp($list);exit();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function add() {

        $this->assign('pro_type',ProductModel::CATEGORY);
        $this->display('form');
    }
    public function edit($id)
    {

        $id = intval($id);
        $product = M('product')->where('id=' . $id)->find();
        if ($product) {
            $this->assign('info', $product);
        } else {
            $this->error('参数错误！');
        }
        $this->assign('pro_type',ProductModel::CATEGORY);
        $this->display('form');
    }

    public function update($id = 0)
    {

        $id = intval($id);
        $data['pro_name'] = isset($_POST['pro_name']) ? $_POST['pro_name'] : false;
        $data['pro_type'] = isset($_POST['pro_type']) ? $_POST['pro_type'] : false;
        $data['pro_m_price'] = isset($_POST['pro_m_price']) ? $_POST['pro_m_price'] : false;
        $data['pro_price'] = isset($_POST['pro_price']) ? $_POST['pro_price'] : '';
        $data['pro_intro'] = isset($_POST['pro_intro']) ? $_POST['pro_intro'] : '';
        $data['thumbnail'] = isset($_POST['thumbnail']) ? $_POST['thumbnail'] : '';
        if (!$data['pro_name'] or !$data['pro_price'] or !$data['pro_intro']) {
            $this->error('警告！产品信息不完整');
        }
        if (!empty($id)) { //编辑
            if (M('product')->data($data)->where('id=' . $id)->save()) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        } else { //增加
            if (M('product')->add($data)) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function del()
    {

        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($id) {
            $map = 'id=' . $id;
            if (M('product')->where($map)->save(['deleted'=>1]) !== false) {
                addlog('删除文章，ID：' . $id);
                $this->success('恭喜，留言删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function export(){

        $bg = isset($_GET['bg']) ? $_GET['bg'] : '';
        $end = isset($_GET['end']) ? $_GET['end'] : false;
        if(empty($bg) || empty($end)){
            $this->error('请选择时间后再导出！');
        }
        $bgTime = $bg." 00:00:00";
        $endTime = $end."  23:59:59";
        $where = "deleted = 0 AND is_loan = 1 AND ctime >= '".$bgTime."' AND ctime <= '".$endTime."'";
        $product = M('product');
        $list = $product->where($where)->field("id,name,mobile,content,from,ctime")->select();
        if(!empty($list)){
            $adviser = M("adviser");
            foreach ($list as $key=> &$val) {
                if(!empty($val['from'])){
                    // 关联顾问表查询关联顾问
                    $val['from'] = $adviser->where(["id"=>$val['from']])->getField("name");
                }else{
                    $val['from'] = "官网";
                }
            }
            $head = array("序号","姓名","电话","需求","来源","时间");
            $filename = "用户贷款信息".date('Ymd');
            $this->exporttocsv($list,$filename,$head);
        }

    }
}